package nl.hu.verbrandendephoenix.controller;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Bid;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Statistics {

	private nl.hu.verbrandendephoenix.view.Statistics statisticsView;
	
	public Statistics() {
		statisticsView = new nl.hu.verbrandendephoenix.view.Statistics(this);
	}

	public Panel getPanel() {
		return statisticsView;
	}
	
	public ClickHandler chBtnDay() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				statisticsView.showDaySelect();
			}
		};		
	}
	public ClickHandler chBtnMonth() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				statisticsView.showMonthSelect();
			}
		};		
	}
	public ClickHandler chBtnYear() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				statisticsView.showYearSelect();
			}
		};		
	}

	public ClickHandler chBtnGoDay() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AppManager.get().getRpcService().getHighestBidDay(statisticsView.getDay(), statisticsView.getMonth(), statisticsView.getYear(), new AsyncCallback<Bid>() {
					@Override
					public void onSuccess(Bid result) {
						VerticalPanel pnlResult = statisticsView.getPanelResult();
						pnlResult.clear();
						if(result == null) {
							pnlResult.add(new Label("Er zijn geen biedingen van deze periode."));
						} else {
							pnlResult.add(new Label("Hoogste bieding: " + result.getPrice()));
						}
					}
					@Override
					public void onFailure(Throwable caught) {
						NotificationManager.get().commError();
					}
				});
			}
		};
	}
	public ClickHandler chBtnGoMonth() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AppManager.get().getRpcService().getHighestBidMonth(statisticsView.getMonth(), statisticsView.getYear(), new AsyncCallback<Bid>() {
					@Override
					public void onSuccess(Bid result) {
						VerticalPanel pnlResult = statisticsView.getPanelResult();
						pnlResult.clear();
						if(result == null) {
							pnlResult.add(new Label("Er zijn geen biedingen van deze periode."));
						} else {
							pnlResult.add(new Label("Hoogste bieding: " + result.getPrice()));
						}
					}
					@Override
					public void onFailure(Throwable caught) {
						NotificationManager.get().commError();
					}
				});
			}
		};	
	}
	public ClickHandler chBtnGoYear() {
		return new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AppManager.get().getRpcService().getHighestBidYear(statisticsView.getYear(), new AsyncCallback<Bid>() {
					@Override
					public void onSuccess(Bid result) {
						VerticalPanel pnlResult = statisticsView.getPanelResult();
						pnlResult.clear();
						if(result == null) {
							pnlResult.add(new Label("Er zijn geen biedingen van deze periode."));
						} else {
							pnlResult.add(new Label("Hoogste bieding: " + result.getPrice()));
						}
					}
					@Override
					public void onFailure(Throwable caught) {
						NotificationManager.get().commError();
					}
				});
			}
		};	
	}
	
}

